package org.mms.controller;

import org.mms.pojo.dto.WarehouseDto;
import org.mms.pojo.entity.Warehouse;
import org.mms.pojo.request.WarehouseRequest;
import org.mms.pojo.util.ResponseMessage;
import org.mms.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/warehouses")
public class WarehouseController {

    @Autowired
    private WarehouseService warehouseService;

    @PostMapping
    public ResponseMessage<WarehouseDto> create(@RequestBody WarehouseRequest request) {
        return ResponseMessage.success(warehouseService.create(request)
                .convert());
    }

    @GetMapping("/{id}")
    public ResponseMessage<WarehouseDto> getById(@PathVariable Long id) {
        return ResponseMessage.success(warehouseService.getById(id)
                .convert());
    }

    @GetMapping
    public ResponseMessage<List<WarehouseDto>> list() {
        return ResponseMessage.success(warehouseService.getAll()
                .stream()
                .map(Warehouse::convert)
                .toList());
    }

    @PutMapping
    public ResponseMessage<WarehouseDto> update(@RequestBody WarehouseRequest request) {
        return ResponseMessage.success(warehouseService.update(request)
                .convert());
    }

    @DeleteMapping("/{id}")
    public ResponseMessage<Void> delete(@PathVariable Long id) {
        warehouseService.delete(id);
        return ResponseMessage.success(null);
    }
}